package com.example.com.gemptc.example;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity{

	private LinkedHashMap<String, HeaderInfo> myDepartments = new LinkedHashMap<String, HeaderInfo>();
	private ArrayList<HeaderInfo> deptList = new ArrayList<HeaderInfo>();

	private MyListAdapter listAdapter;
	private ExpandableListView myList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//添加测试数据
		loadData();

		// get reference to the ExpandableListView
		myList = (ExpandableListView) findViewById(R.id.myList);
		myList.setDividerHeight(0);//取消横线
		// create the adapter by passing your ArrayList data
		listAdapter = new MyListAdapter(MainActivity.this, deptList);
		// attach the adapter to the list
		myList.setAdapter(listAdapter);

		//展开所有组
		expandAll();

		// listener for child row click
		myList.setOnChildClickListener(myListItemClicked);
		// listener for group heading click
		myList.setOnGroupClickListener(myListGroupClicked);

	}

	//展开所有的组
	private void expandAll() {
		int count = listAdapter.getGroupCount();
		for (int i = 0; i < count; i++) {
			myList.expandGroup(i);
		}
	}

	//收缩所有组
	private void collapseAll() {
		int count = listAdapter.getGroupCount();
		for (int i = 0; i < count; i++) {
			myList.collapseGroup(i);
		}
	}

	// load some initial data into out list
	private void loadData() {

		addProduct("好友", "张三");
		addProduct("好友", "李四");
		addProduct("好友", "王五");

		addProduct("同学", "王尼玛");
		addProduct("同学", "王大锤");

	}

	//组成员点击事件
	private OnChildClickListener myListItemClicked = new OnChildClickListener() {

		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {

			// get the group header
			HeaderInfo headerInfo = deptList.get(groupPosition);
			// get the child info
			DetailInfo detailInfo = headerInfo.getProductList().get(
					childPosition);
			// display it or do something with it
			Toast.makeText(
					getBaseContext(),
					"Clicked on Detail " + headerInfo.getName() + "/"
							+ detailInfo.getName(), Toast.LENGTH_LONG).show();
			return false;
		}

	};

	// 组点击事件
	private OnGroupClickListener myListGroupClicked = new OnGroupClickListener() {

		public boolean onGroupClick(ExpandableListView parent, View v,
				int groupPosition, long id) {

			// get the group header
			HeaderInfo headerInfo = deptList.get(groupPosition);
			// display it or do something with it
			Toast.makeText(getBaseContext(),
					"Child on Header " + headerInfo.getName(),
					Toast.LENGTH_LONG).show();

			return false;
		}

	};

	//添加组、成员
	private int addProduct(String department, String product) {

		int groupPosition = 0;

		//组存在则不添加
		HeaderInfo headerInfo = myDepartments.get(department);
		// add the group if doesn't exists
		if (headerInfo == null) {
			headerInfo = new HeaderInfo();
			headerInfo.setName(department);
			myDepartments.put(department, headerInfo);
			deptList.add(headerInfo);
		}

		//获取组成员集合
		ArrayList<DetailInfo> productList = headerInfo.getProductList();
		//获取组成员数量
		int listSize = productList.size();
		// +1
		listSize++;

		//向组添加成员bean
		DetailInfo detailInfo = new DetailInfo();
		detailInfo.setSequence(String.valueOf(listSize));
		detailInfo.setName(product);
		productList.add(detailInfo);
		headerInfo.setProductList(productList);

		// 返回组的下标
		groupPosition = deptList.indexOf(headerInfo);
		return groupPosition;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
